package org.kl.bpm.service.conf;

import java.util.List;
import java.util.Map;

import org.kl.bf.utils.CRUDUtils;
import org.kl.bpm.entity.conf.BpmConfForm;
import org.kl.bpm.repository.conf.BpmConfFormDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

/**
 * 
 * @Author hqm
 * @CreateTime 2014年5月25日 下午8:40:29
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class BpmConfFormService {

	private BpmConfFormDao bpmConfFormDao;

	public BpmConfForm getBpmConfForm(String id) {
		return bpmConfFormDao.findOne(id);
	}

	public List<BpmConfForm> findByBpmConfNodeId(String bpmConfNodeId) {
		return bpmConfFormDao.findByBpmConfNodeId(bpmConfNodeId);
	}

	public List<BpmConfForm> findByBpmConfNodeIdAndType(String bpmConfNodeId, Integer type) {
		return bpmConfFormDao.findByBpmConfNodeIdAndType(bpmConfNodeId, type);
	}

	public List<BpmConfForm> findByBpmConfNodeIdAndOriginValue(String bpmConfNodeId, String originValue) {
		return bpmConfFormDao.findByBpmConfNodeIdAndOriginValue(bpmConfNodeId, originValue);
	}

	/**
	 * 通过XML中原始FormKey,节点ID,流程定义ID 获取表单配置
	 * 
	 * @param originValue
	 * @param code
	 * @return
	 */
	public BpmConfForm findByOriginValueAndBpmConfNodeCodeAndBpmConfNodeBpmConfBaseProcessDefinitionId(String originValue, String code,
			String processDefinitionId) {
		return bpmConfFormDao.findByOriginValueAndBpmConfNodeCodeAndBpmConfNodeBpmConfBaseProcessDefinitionId(originValue, code,
				processDefinitionId);
	}

	/**
	 * 获取内部表单
	 * 
	 * @param processDefinitionId
	 * @param nodeCode
	 * @return
	 */
	public BpmConfForm findByBpmConfNodeBpmConfBaseProcessDefinitionIdAndBpmConfNodeCode(String processDefinitionId, String nodeCode) {
		List<BpmConfForm> bcfList = bpmConfFormDao.findByBpmConfNodeBpmConfBaseProcessDefinitionIdAndBpmConfNodeCode(processDefinitionId,
				nodeCode);
		for (BpmConfForm bcf : bcfList) {
			if (bcf.getType() != null && bcf.getType() == 0) {// 取出内部表单
				return bcf;
			}
		}
		return null;
	}

	/**
	 * 获取表单
	 * 
	 * @param processDefinitionId
	 * @param nodeCode
	 * @return
	 */
	public List<BpmConfForm> findByBpmConfNodeBpmConfBaseProcessDefinitionIdAndBpmConfNodeCodeAndTypeOrderByCreateTimeAsc(String processDefinitionId,
			String nodeCode, Integer type) {
		return bpmConfFormDao.findByBpmConfNodeBpmConfBaseProcessDefinitionIdAndBpmConfNodeCodeAndTypeOrderByCreateTimeAsc(processDefinitionId, nodeCode, type);
	}

	public void save(BpmConfForm entity) {
		bpmConfFormDao.save(entity);
	}

	public void delete(String id) {
		bpmConfFormDao.delete(id);
	}

	public void delete(BpmConfForm entity) {
		bpmConfFormDao.delete(entity);
	}

	public List<BpmConfForm> getAllBpmConfForm() {
		return bpmConfFormDao.findAll();
	}
	//带条件的查询
	public List<BpmConfForm> getAllBpmConfForm(Map<String, Object> searchParams,String sortType, String orderType) {
		return bpmConfFormDao.searchAll(searchParams, sortType, orderType, BpmConfForm.class);
	}
	

	public Page<BpmConfForm> getUserBpmConfForm(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<BpmConfForm> spec = buildSpecification(searchParams);

		return bpmConfFormDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<BpmConfForm> buildSpecification(Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		Specification<BpmConfForm> spec = DynamicSpecifications.bySearchFilter(filters.values(), BpmConfForm.class);
		return spec;
	}

	@Autowired
	public void setBpmConfFormDao(BpmConfFormDao bpmConfFormDao) {
		this.bpmConfFormDao = bpmConfFormDao;
	}

}
